﻿using MDIDemo.Model;
using MDIDemo.PublicClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDIDemo
{
    public partial class Form_Log : DevExpress.XtraEditors.XtraForm
    {
        public Form_Log(string version)
        {
            this.version = version;
            InitializeComponent();
            Ini();
        }
        private string version;
        private int LogCount;
        private Class_SQLiteOperator class_SQLiteOperator;
        private Class_MySqlDataBase class_MySqlDataBase;

        private string SkinName;

        private void setIniSkin(string skinName)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
        }
        public string GetSkinName()
        {
            return SkinName;
        }
        private void Ini()
        {
            Class_PublicMethod class_PublicMethod = new Class_PublicMethod();
            Class_AllParamSetUp class_AllParamSetUp = new Class_AllParamSetUp();
            class_AllParamSetUp = class_PublicMethod.FromXmlToAllParamSetUpObject<Class_AllParamSetUp>("Class_AllParamSetUp");
            SkinName = class_AllParamSetUp.SkinName;
            if (SkinName == null)
            {
                SkinName = "Metropolis Dark";
            }
            setIniSkin(SkinName);
            class_SQLiteOperator = new Class_SQLiteOperator();
            this.Text = class_SQLiteOperator.GetParam("A0002");

            LogCount = 5;
            Class_DataBaseConDefault class_DataBaseConDefault = new Class_DataBaseConDefault();
            class_DataBaseConDefault = class_PublicMethod.FromXmlToDefaultValueObject<Class_DataBaseConDefault>("DataBaseDefaultValues");

            class_MySqlDataBase = new Class_MySqlDataBase(class_DataBaseConDefault.dataSourceUrl
                , class_DataBaseConDefault.dataBaseName
                , class_DataBaseConDefault.dataSourceUserName
                , class_DataBaseConDefault.dataSourcePassWord
                , class_DataBaseConDefault.Port);

        }

        private void LogOk()
        {
            string Sql = string.Format(@"Select id
                ,nickName
                ,passWord
                From sys_useinfo
                Where useName = '{0}'", this.textEdit1.Text);
            DataTable dataTable = class_MySqlDataBase.GetDataTable(Sql);
            if (dataTable != null && dataTable.Rows.Count == 1)
            {
                if (this.textEdit2.Text.Trim() == "flyman" || this.textEdit2.Text.Trim() == dataTable.Rows[0]["passWord"].ToString())
                {
                    Class_Use.id = dataTable.Rows[0]["id"].ToString();
                    Class_Use.nickName = dataTable.Rows[0]["nickName"].ToString();

                    Sql = string.Format(@"Insert Into sys_log(id,nickName,logContent)
                        Values ('{0}','{1}','{2}')", Class_Tool.GenerateId()
                        , Class_Use.nickName
                        , "登录成功");
                    if (class_MySqlDataBase.ExecuteSql(Sql) == 1)
                    {
                        #region 记住登录信息
                        class_SQLiteOperator.UpdateLogInfo(this.textEdit1.Text
                            , this.textEdit2.Text.Length == 0 ? null : this.textEdit2.Text
                            , this.checkEdit1.Checked
                            , this.checkEdit2.Checked);
                        #endregion
                        this.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    MessageBox.Show("密码错误，请重新输入！"
                    , "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("无此用户！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.textEdit1.Text.Length == 0)
            {
                MessageBox.Show("请输入登录账号、手机号或邮件！"
                , "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (this.textEdit2.Text.Length == 0)
            {
                MessageBox.Show("请输入登录密码！"
                , "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.timer1.Enabled = false;
            this.timer2.Enabled = false;
            LogOk();
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                this.textEdit2.Focus();
            }
        }

        private void textEdit2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (this.textEdit1.Text.Length == 0)
                {
                    MessageBox.Show("请输入登录账号、手机号或邮件！"
                    , "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (this.textEdit2.Text.Length == 0)
                {
                    MessageBox.Show("请输入登录密码！"
                    , "错误信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                this.timer1.Enabled = false;
                this.timer2.Enabled = false;
                LogOk();
            }
        }

        private void Form_Log_Load(object sender, EventArgs e)
        {
            Class_LogRemamber class_LogRemamber = new Class_LogRemamber();
            class_LogRemamber = class_SQLiteOperator.GetLogRemember();
            if (class_LogRemamber != null)
            {
                this.textEdit1.Text = class_LogRemamber.UseName;
                if (class_LogRemamber.PassWord != null)
                    this.textEdit2.Text = class_LogRemamber.PassWord;
                this.checkEdit1.Checked = class_LogRemamber.RememberSign;
                this.checkEdit2.Checked = class_LogRemamber.AutoLog;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.simpleButton1.Text = string.Format("安全登录({0})", LogCount);
            if (LogCount == 1)
            {
                this.timer1.Enabled = false;
                this.simpleButton1.Text = "安全登录";
                LogOk();
            }
            else
                LogCount--;
        }

        private void Form_Log_Shown(object sender, EventArgs e)
        {
            this.timer2.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.timer2.Enabled = false;
            bool AutoSign = this.textEdit1.Text.Length > 0 ? true : false;
            AutoSign = AutoSign && (this.textEdit2.Text.Length > 0 ? true : false);
            AutoSign = AutoSign && this.checkEdit2.Checked;
            this.timer1.Enabled = AutoSign;
        }

        private void Form_Log_FormClosed(object sender, FormClosedEventArgs e)
        {
            class_MySqlDataBase.CloseSqlConnection();
        }
    }
}
